﻿using Commen;
using DbF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AyaWo.SpiderService
{
    public partial class ConnForm : Form
    {
        public ConnForm()
        {
            InitializeComponent();
            try
            {
                var va = DatHelper<string>.GetDatStr("connStr.dat");
                textBox1.Text = va;
            }
            catch {
                textBox1.Text = "";
            }
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            string connStr=textBox1.Text.Trim();
            DbHelper dh = new DbHelper(connStr, SqlSourceType.MySql);
            try
            {
                dh.OpenConnection();
                MessageBox.Show("连接字符串正确....");
            }
            catch
            {
                MessageBox.Show("连接字符串错误....");
            }
            finally
            {
                dh.Dispose();
            }
        }

        /// <summary>
        /// 保存连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string connStr = textBox1.Text.Trim();
                DatHelper<string>.SaveDat("connStr.dat", connStr);
                DatHelper<string>.SaveDat("AutoConnStr.dat", connStr);
       DbHelper dh = new DbHelper(connStr, SqlSourceType.MySql);
                try
                {
                    dh.OpenConnection();
                }
                catch
                {
                    MessageBox.Show("连接字符串错误....");
                    return;
                }
                try
                {
                    string sqlStr = "CREATE TABLE `spcontent` (  `ID` varchar(50) NOT NULL,  `WebName` varchar(200) DEFAULT NULL,  `WebUrl` varchar(255) DEFAULT NULL, `addr` varchar(255) DEFAULT NULL,  `InUrl` varchar(255) DEFAULT NULL,  `CiShu` int(11) DEFAULT NULL,`caogao` int(11) DEFAULT NULL,  `Title` varchar(255) DEFAULT NULL,  `Source` varchar(255) DEFAULT NULL, `typ` varchar(50) DEFAULT NULL, `Autos` varchar(255) DEFAULT NULL,  `BuileDate` varchar(255) DEFAULT NULL,  `Content` varchar(255) DEFAULT NULL,  `list` varchar(255) DEFAULT NULL,  PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
                    string content = "CREATE TABLE `content` (  `ID` varchar(50) NOT NULL,  `Url` varchar(255) DEFAULT NULL,  `Title` varchar(255) DEFAULT NULL,  `Source` varchar(255) DEFAULT NULL,  `Autos` varchar(255) DEFAULT NULL,  `BuileDate` varchar(255) DEFAULT NULL,  `Content` text,  PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

                    dh.ExecuteBySql(new StringBuilder(sqlStr));
                    dh.ExecuteBySql(new StringBuilder(content));
                }
                catch
                {

                }
            
                MessageBox.Show("保存成功....");
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch
            {
                MessageBox.Show("保存失败....");
            }

        }

        private void ConnForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //var tt = MessageBox.Show("确定要结束运用吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (tt == DialogResult.Yes)
            //{
            //   Application.Exit();
            //}
            //else
            //{
            //    DialogResult = DialogResult.Cancel;
            //}
        }
    }
}
